#include "video.h"
#include "GL/glfw.h"
#include "common.h"
#include <iostream>

/*==============================================================================
			Constructor initializes GLWF library and opens new window
==============================================================================*/
Video::Video()
{
	if (glfwInit() == GL_TRUE)
		LOG_INFO("GLFW library initialized succesfully");
	else
	{
		LOG_ERROR("Couldn't initialize GLFW library...");
		return;
	}

// ToDo: choose fulscreen or normal window
	if (glfwOpenWindow(WINDOW_WIDTH, WINDOW_HEIGHT, 8, 8, 8, 8, 0, 0, WINDOW_MODE) == GL_TRUE)
		LOG_INFO("GLFW window created succesfully");
	else
	{
		LOG_ERROR("Couldn't create GLFW window...");
		glfwTerminate();
		return;
	}
	glfwSetWindowTitle(APP_NAME);

	// Enable alpha channel in drawing
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}

/*==============================================================================
					Destructor terminates GLFW library
==============================================================================*/
Video::~Video()
{
	LOG_INFO("Terminating GLFW...");
	glfwTerminate();
}

/*==============================================================================
			Draws a triangle. Must be given 3 vertices and color
==============================================================================*/
void Video::DrawTriangle(const Vector2& a, const Vector2& b, const Vector2& c, const Color& col)
{
	float col_r, col_g, col_b, col_a;
	col.Normalize(col_r, col_g, col_b, col_a);

	//a.ToOpenglCoords();
	///b.ToOpenglCoords();
	//c.ToOpenglCoords();

	glBegin(GL_TRIANGLES);
		glColor4f(col_r, col_g, col_b, col_a);
		glVertex2f(a.x, a.y);
		glVertex2f(b.x, b.y);
		glVertex2f(c.x, c.y);
	glEnd();
}

/*==============================================================================
   Draws rectangle. Must be given top-left and bottom-right corners and color
==============================================================================*/
void Video::DrawRectangle(const Vector2& tl, const Vector2& br, const Color& col)
{
	float col_r, col_g, col_b, col_a;
	col.Normalize(col_r, col_g, col_b, col_a);

	//tl.ToOpenglCoords();
	//br.ToOpenglCoords();

	glBegin(GL_QUADS);
		glColor4f(col_r, col_g, col_b, col_a);
		glVertex2f(tl.x, tl.y + br.y);
		glVertex2f(tl.x + br.x, tl.y + br.y);
		glVertex2f(tl.x + br.x, tl.y);
		glVertex2f(tl.x, tl.y);
	glEnd();
}

/*==============================================================================
						Draw coloured line
==============================================================================*/
void Video::DrawLine(const Vector2& start, const Vector2& end, const Color& col)
{
	float col_r, col_g, col_b, col_a;
	col.Normalize(col_r, col_g, col_b, col_a);

	glBegin(GL_LINES);
		glColor4f(col_r, col_g, col_b, col_a);
		glVertex2f(start.x, start.y);
		glVertex2f(end.x, end.y);
	glEnd();
}

/*==============================================================================
						Draws one color background
==============================================================================*/
void Video::DrawBackground(const Color& col)
{
	float col_r, col_g, col_b, col_a;
	col.Normalize(col_r, col_g, col_b, col_a);
	glClearColor(col_r, col_g, col_b, col_a);
}

/*==============================================================================
								Do rendering
==============================================================================*/
void Video::Render()
{
	glfwSwapBuffers();

	glClear(GL_COLOR_BUFFER_BIT);
}

/*==============================================================================
				True, if ESC key pressed or Close button was pressed
==============================================================================*/
bool Video::TellsQuit()
{
	return glfwGetKey(GLFW_KEY_ESC) || !glfwGetWindowParam(GLFW_OPENED);
}
